package com.everydayflash.cart.mods {
	import net.hires.debug.Stats;
	
	import com.as3dmod.ModifierStack;
	import com.as3dmod.modifiers.Perlin;
	import com.as3dmod.util.bitmap.PerlinNoise;
	import com.everydayflash.cart.core.AbstractSketch;
	import com.everydayflash.cart.core.Pixel3D;
	import com.everydayflash.cart.mods.plugin.LibrarySimple;
	
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.events.MouseEvent;
	import flash.geom.Matrix3D;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Vector3D;		

	/**
	 * @author bartekd
	 */
	public class ModPerlin extends AbstractSketch {
		
		private var pts:Vector.<Pixel3D>;
		
		private var projection:Matrix3D;
		private var angleX:Number = 0;		private var angleY:Number = 0;
		private var matrix:Matrix3D;
		private var stack:ModifierStack;

		override protected function init():void {
			addChild(new Stats());
			
			var p:PerspectiveProjection = new PerspectiveProjection();
			p.focalLength = 400;
			projection = p.toMatrix3D();
			matrix = new Matrix3D();
	
			pts = new Vector.<Pixel3D>();

			var d:Number = 10;
			var n:Number = 10000;

			for (var i:int = 0; i < n; i++) {
				
				var r:Pixel3D = new Pixel3D(i / 50 * d - 800 + Math.random() * 3, 0, i % 50 * d - 250 + Math.random() * 3);
				r.color = 0xffffff;
				pts.push(r);
			}
			
			stack = new ModifierStack(new LibrarySimple(), pts);
			
			var perlin:Perlin = new Perlin(.5, new PerlinNoise(60, 60));
//			addChild(perlin.source.bitmap); // 
			perlin.speedX = .5;			perlin.speedY = .5;
			perlin.source.octaves = 2;
			perlin.source.baseX = 15;
			perlin.source.baseY = 15; 
			perlin.source.channels = BitmapDataChannel.GREEN;
			stack.addModifier(perlin);
		}

		override protected function mouseDown(e:MouseEvent):void {
		}

		override protected function draw():void {
			stack.apply();
			
			clearBCanvas();
//			bcanvas.applyFilter(bcanvas, bcanvas.rect, new Point(), new BlurFilter(4,4,1));
//			bcanvas.colorTransform(bcanvas.rect, new ColorTransform(1,1,1,.8));

			angleX = (mouseX - sw/2) / sw/2 * 100;			angleY = (mouseY - sh/2) / sh/2 * 100;

			

			for (var i:int = 0;i < pts.length; i++) {
				matrix.identity();
				matrix.appendRotation(angleX, new Vector3D(0, 1, 0));
				matrix.appendRotation(angleY, new Vector3D(1, 0, 0));
				matrix.appendTranslation(0, 0, 350);
			
				var v:Pixel3D = pts[i];
				
				var iv:Vector3D = matrix.transformVector(v);
				
				var vz:Number = 1 - (iv.z / 500);
				vz = Math.min(1, vz);
				vz = Math.max(0, vz);
				
				iv = projection.transformVector(iv);
				iv.project();

				var c:uint = (vz * 255) << 24 | v.color;
				
				bcanvas.setPixel32(sw / 2 + iv.x, sh/2 + iv.y, c);//				bcanvas.setPixel32(sw / 2 + iv.x + 1, sh/2 + iv.y, c);//				bcanvas.setPixel32(sw / 2 + iv.x, sh/2 + iv.y + 1, c);//				bcanvas.setPixel32(sw / 2 + iv.x - 1, sh/2 + iv.y, c);//				bcanvas.setPixel32(sw / 2 + iv.x, sh/2 + iv.y - 1, c);
			}
		}
	}
}
